
var reqUI='#request';
var resUI='#response';


var mijaxRun = function(action, url, data, successFunc, errorFunc, isAsync, timeOutVal, dataType) {
	if (!dataType) { // when it is not define
		dataType = 'json'; // default to json datatype.
	}

	var isDbg = miShare(isDebug);
	if (!errorFunc) { // when it is not define
		errorFunc = function(xhr, textStatus) {
			if (xhr && xhr.status == 200) {
				return;
			}
			if (isDbg) {
				console.log("While accessing: " + url
						+ "\nAn error occured.\nstatus code:" + xhr.status
						+ ";\nmessage:\n" + xhr.statusText);
				console.log('xhr:');
				console.log(xhr);
				console.log('textStatus:');
				console.log(textStatus);
			}
		};
	}
	// DO NOT use async in a page with multiple call. Use sync call whenever
	// possible. this will avoid hanging the browser being executed too fast
	// when there is
	// multiple calls.
	var isAsyncCall = false;
	if (isAsync) {
		// ### 2012-Oct-17, bp.ong: new isAsyncCall is
		// true to avoid UI freeze.
		isAsyncCall = isAsync;
	}
	var timeOutDefault = 15000; // 15 seconds.
	// ### 2012-Oct-17, bp.ong: new timeout parameter is added to avoid UI
	// freeze.
	if (timeOutVal) {
		timeOutDefault = timeOutVal;
	}

	var requestInfo = {};
	var beforeSendFunc = function(xhr, data) {
		requestInfo = data;
		if(isDbg){
			console.log('xhr:');
			console.log(xhr);
			console.log('settings:');
			console.log(data);
		}
		miShare(beforeSendXHR, xhr);
		miShare(beforeSendDATA, data);
	};

	var contentType = "application/json; charset=\"utf-8\"";
	if (dataType === 'xml') {
		contentType = "application/xml; charset=utf-8";
	}
	var ajax = {
		type : action,
		url : url,
		dataType : dataType,
		contentType : contentType,
		data : data,
		beforeSend : beforeSendFunc,
		complete : successFunc,
		error : errorFunc,
		async : isAsyncCall,
		timeout : timeOutDefault
	};// end of ajax.
	$.ajax(ajax); // sending the ajax request to server
	return requestInfo;
};

var ping = function() {
	var pingText = $('#txtPing').val();
	var url = '../rest/echo/ping/' + pingText;
	var data = {};
	var reqMsg =mijaxRun('GET', url, data, function(resp, status) {
		if(!resp){
			console.log('Undefined Response From URL:'+url);
		}
		var msg='Ping replied:\n'+resp.responseText;
		console.log(msg);
		$(resUI).val(msg);
	}, function(resp, status) {
		var msg='Failure to send ping:'+status;
		console.log(msg);
		$(resUI).val(msg);
	});
	$(reqUI).val(JSON.stringify(reqMsg));
};

var pingXml = function() {
	var pingText = $('#txtPing').val();
	var url = '../rest/echo/ping/' + pingText;
	var data = {};
	// (action, url, data, successFunc, errorFunc,	isAsync, timeOutVal, dataType) {
	var reqMsg =mijaxRun('GET', url, data, function(resp, status) {
		if(!resp){
			console.log('Undefined Response From URL:'+url);
		}
		var msg='PingXml replied:\n'+resp.responseText;
		console.log(msg);
		$(resUI).val(msg);
	}, function(resp, status) {
		var msg='Failure to send pingXml:'+status;
		console.log(msg);
		$(resUI).val(msg);
	}, false, 15000, 'xml');
	$(reqUI).val(JSON.stringify(reqMsg));
};

var dump = function() {
	var url = '../rest/echo/dump';
	var data = {};
	var reqMsg = getjax(url, data, function(resp) {
		if(!resp){
			console.log('Undefined Response From URL:'+url);
		}
		var msg='dump replied:\n';
		console.log(msg);
		console.log(resp);
		$(resUI).val(msg+JSON.stringify(resp));
	}, function(resp, status) {
		var msg='Failure to send dump:'+status;
		console.log(msg);
		$(resUI).val(msg);
	});
	$(reqUI).val(JSON.stringify(reqMsg));
};

var dumpXml = function() {
	var url = '../rest/echo/dump';
	var data = {};
	var reqMsg = getjax(url, data, function(resp) {
		if(!resp){
			console.log('Undefined Response From URL:'+url);
		}
		var msg='dumpXml replied:\n';
		console.log(msg);
		console.log(resp);
		$(resUI).val(msg+resp.documentElement.outerHTML);
	}, function(resp, status) {
		var msg='Failure to send dumpXml:'+status;
		console.log(msg);
		$(resUI).val(msg);
	}, false, 15000, 'xml');
	$(reqUI).val(JSON.stringify(reqMsg));
};
